package com.ky.manage.utils.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.ZyUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.ByteArrayEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;

/**
 * 接口请求工具类
 */
public class AsyncHttpUtils {
    private static AsyncHttpUtils mInstance;

    private AsyncHttpUtils() {
    }

    public static AsyncHttpUtils getInstance() {
        if (mInstance == null) {
            synchronized (AsyncHttpUtils.class) {
                if (mInstance == null) {
                    mInstance = new AsyncHttpUtils();
                }
            }
        }
        return mInstance;
    }

    private AsyncHttpClient initHttpClient() {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(15000);
        String token = BusinessHttpReqUtils.getInstance().getToken();
        Log.d("lhm", "token = " + token);
        if (!TextUtils.isEmpty(token)) {
            client.addHeader("Authorization", token);
        }
        return client;
    }

    public void get(String url, TextHttpResponseHandler responseHandler) {
        AsyncHttpClient client = initHttpClient();
        client.get(url, responseHandler);
    }

    public void get(Context context, String url, TextHttpResponseHandler responseHandler) {
        AsyncHttpClient client = initHttpClient();
        client.get(context, url, responseHandler);
    }

    public void get(String url, RequestParams params, TextHttpResponseHandler responseHandler) {
        AsyncHttpClient client = initHttpClient();
        client.get(url, params, responseHandler);
    }

    public void get(Context context, String url, RequestParams params, TextHttpResponseHandler responseHandler) {
        AsyncHttpClient client = initHttpClient();
        client.get(context, url, params, responseHandler);
    }

    public void get(Context context, String url, Header[] headers, RequestParams params,
                    TextHttpResponseHandler responseHandler) {
        AsyncHttpClient client = initHttpClient();
        client.get(context, url, headers, params, responseHandler);

    }

    public void put(Context context, String url, RequestParams requestParams, TextHttpResponseHandler responseHandler) {
        if (null == requestParams) {
            ToastUtils.showRoundRectToast("参数不能为空");
            return;
        }
        AsyncHttpClient client = initHttpClient();
        try {
            client.put(context, url, requestParams, responseHandler);
        }  catch (Exception e) {

        }
    }

    public void put(Context context, String url, JSONObject jsonObject, TextHttpResponseHandler responseHandler) {
        if (null == jsonObject) {
            ToastUtils.showRoundRectToast("参数不能为空");
            return;
        }
        AsyncHttpClient client = initHttpClient();
        String contentType = "application/json";
        ByteArrayEntity entity = null;
        try {
            entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, contentType));
            client.put(context, url, entity, contentType, responseHandler);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {

        }
    }

    /**
     * post json请求
     * @param context
     * @param url
     * @param jsonObject
     * @param responseHandler
     */
    public void postJson(Context context, String url, JSONObject jsonObject,
                                TextHttpResponseHandler responseHandler){
        if (null == jsonObject) {
            ToastUtils.showRoundRectToast("参数不能为空");
            if (null != responseHandler) {
                responseHandler.onFailure(-1, null, "参数不能为空", null);
            }
            return;
        }
        AsyncHttpClient client = initHttpClient();
        String contentType = "application/json";
        ByteArrayEntity entity = null;
        try {
            ZyUtils.getInstance().insertLog("zhaoyan", "postJson jsonObject = " + jsonObject.toString());
            entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, contentType));
            client.post(context, url, entity, contentType, responseHandler);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {

        }
    }

    public void uploadFile(String url, RequestParams requestParams, TextHttpResponseHandler callback) {
        AsyncHttpClient client = initHttpClient();
        client.post(url, requestParams, callback);
    }

    public void post(String url, TextHttpResponseHandler callback) {
        AsyncHttpClient client = initHttpClient();
        client.post(url, callback);

    }

    public void post(String url, RequestParams requestParams, TextHttpResponseHandler callback) {
        AsyncHttpClient client = initHttpClient();
        client.post(url, requestParams, callback);

    }

    public void delete(String url, TextHttpResponseHandler callback) {
        AsyncHttpClient client = initHttpClient();
        try {
            client.delete(url, callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
